package com.ke.jdyd;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.ke.jdyd.ui.LoginActivity;
import com.ke.jdyd.ui.MainActivity;

import java.lang.ref.WeakReference;

public class SplashActivity extends AppCompatActivity {
  private final static int LOGIN = 1;
  private final static int HOME = 2;


  private LoginHandler mHandler;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);
    mHandler = new LoginHandler(this);
    SharedPreferences preferences = getSharedPreferences("jdyd", MODE_PRIVATE);
    String user_token = preferences.getString("user_token", "");
    if (TextUtils.isEmpty(user_token)) {
      mHandler.sendEmptyMessageDelayed(LOGIN, 1500);
    } else {
      mHandler.sendEmptyMessageDelayed(HOME, 1500);
    }

  }
  static class  LoginHandler extends Handler{
    private WeakReference<Activity> weakReference;

    public LoginHandler(Activity context) {
      this.weakReference = new WeakReference<>(context);
    }

    @Override
    public void handleMessage(@NonNull Message msg) {
      super.handleMessage(msg);
      Activity context = weakReference.get();
      if (context == null ) return;
      if (msg.what == HOME) {
        context.startActivity(new Intent(context, MainActivity.class));
      } else {
        context.startActivity(new Intent(context, LoginActivity.class));
      }
      context.finish();
    }
  }
}
